package cn.edu.jxau.core.util;

import cn.edu.jxau.core.process.spectrumutil.PretreatmentAlgorithmException;
import cn.edu.jxau.core.util.upload.FileUpload;
import cn.edu.jxau.core.vo.Point;
import cn.edu.jxau.core.vo.SpectrumFile;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 将光谱文件处理为光谱文件类型的json文件
 *
 * @author 巫发萍
 * SpectrumToJson
 * 2017年8月9日
 */
public class SpectrumToJson {
    private File spectrum;
    private Logger logger = LoggerFactory.getLogger(getClass());
    SpectrumFile spectrumfile = null;

    /**
     * 初始化ToJson
     *
     * @param spectrum：算法文件地址
     */
    public SpectrumToJson(File spectrum) {
        this.spectrum = spectrum;
    }

    public String executeToJson() throws PretreatmentAlgorithmException, IOException {
        if (spectrumfile == null) {
            executeToObject();
        }
        return JSON.toJSONString(spectrumfile);
    }

    /**
     * 这是一个新的光谱文件，开始序列化。
     * @return 光谱文件对象
     * @throws PretreatmentAlgorithmException
     * @throws IOException
     */
    public SpectrumFile executeToObject() throws PretreatmentAlgorithmException, IOException {

        if (spectrumfile == null) spectrumfile = new SpectrumFile();
        List<Point> pointlist = new ArrayList<>();
        List<String> list = new ArrayList<>();
        try (  // 创建字符输出流,自动关闭资源
                 FileReader fr = new FileReader(spectrum)
        ) {
            BufferedReader br = new BufferedReader(fr);
            //用于保存实际读取的字符数
            String hasRead = null;
            //使用循环读取数据
            while ((hasRead = br.readLine()) != null) {
                list.add(hasRead);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (list == null) {
            throw new PretreatmentAlgorithmException("文件不能为空");
        }
        // 插入数据
        try {
            for (String s : list) {
                String[] split = s.split("\t");
                Point point = new Point(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
                pointlist.add(point);
            }
        } catch (NumberFormatException e) {
            throw new PretreatmentAlgorithmException("文件格式错误！！", e);
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new PretreatmentAlgorithmException("文件夹格式错误", e);
        } catch (Exception e) {
            throw new PretreatmentAlgorithmException("解析文件异常");
        }

        spectrumfile.setPoints(pointlist);

        spectrumfile.flush();
        spectrumfile.setFileDir(spectrum.getParent().replace(FileUpload.getBaseABSPath(),""));
        String fileName = spectrum.getName();
        spectrumfile.setSpectrumName(fileName.substring(fileName.lastIndexOf(FileUpload.SEPERATOR) + 3, fileName.length()));
        // 序列化保存对象
        spectrumfile = SerializaObject.writeObject(spectrumfile);
        return spectrumfile;
    }

}
